package crack;

import org.junit.Test;

import java.util.LinkedList;

/**
 * @author fance
 * @date 18-4-8 下午3:42
 */
public class StackAndQueue {

    public void hanoi(int n) {
        if (n > 0) {
            func(n, "left", "mid", "right");
        }
    }
    private void func(int n, String from, String mid, String to) {
        if (n == 1) {
            System.out.println("move from " + from + " to " + to);
        } else {
            func(n - 1, from, to, mid);
            func(1, from, mid, to);
            func(n - 1, mid, from, to);
        }
    }
    @Test
    public void test() {
        hanoi(3);
    }
}



class MinStack {
    private LinkedList<Integer> data;
    private LinkedList<Integer> min;

    public MinStack(LinkedList<Integer> data, LinkedList<Integer> min) {
        this.data = data;
        this.min = min;
    }
    public void push(int item) {
        if (min.isEmpty()) {
            min.push(item);
        } else if (item <= getMin()) {
            min.push(item);
        }
        data.push(item);
    }

    public int pop() {
        if (data.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        int val = data.pop();
        if (val == getMin()) {
            min.pop();
        }
        return val;
    }
    public int getMin() {
        if (min.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        return min.peek();
    }
}


class TwoStackQueue {
    private LinkedList<Integer> stackPush;
    private LinkedList<Integer> stackPop;

    public TwoStackQueue(LinkedList<Integer> stackPush, LinkedList<Integer> stackPop) {
        this.stackPush = stackPush;
        this.stackPop = stackPop;
    }

    public void offer(int val) {
        stackPush.push(val);
    }

    public int poll() {
        if (stackPop.isEmpty() && stackPush.isEmpty()) {
            throw new RuntimeException("queue is empty");
        } else if (stackPop.isEmpty()){
            while (!stackPush.isEmpty()) {
                stackPop.push(stackPush.pop());
            }
        }
        return stackPop.pop();
    }

    public int peek() {
        if (stackPop.isEmpty() && stackPush.isEmpty()) {
            throw new RuntimeException("queue is empty");
        } else if (stackPop.isEmpty()){
            while (!stackPush.isEmpty()) {
                stackPop.push(stackPush.pop());
            }
        }
        return stackPop.peek();
    }
}
